local M = {}

---Usage example:
--- <pre>lua
---require("installLib").install("yay", {"alacritty"})
--- </pre>
---@generic T: table
---@param manager string Package manager
---@param packages T Packages list
function M.install(manager, packages)
	for _, v in pairs(packages) do
		local output = M._commandOutput("pacman -Q | grep " .. v)

		if string.len(output) == 0 or not string.match(output, "%s") then
			print("start install " .. v)
			os.execute(manager .. " -S --noconfirm " .. v)
		end
	end
end

---@param command string shell command
---@return string
function M._commandOutput(command)
	local f = io.popen(command)
	local output = ""
	if f then
		output = f:read("*a")
		f:close()
	end
	print(output)
	return output
end

return M
